<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../base.jsp" %>
<!-- Select2 -->
<!-- Select2 -->
<link rel="stylesheet" href="${url }plugins/select2/select2.min.css">
<script src="${url }plugins/select2/select2.full.min.js"></script>
<style>
	.content-wrapper{margin-left: 0px}
</style>
<script type="text/javascript">
var keyArray =new Array();
$(function() {
	init();
});

function init(){
	$.ajax({
		type:"POST",
		url: path+'manager/getNodes',
		success: function(result){
			$("#example1").html("");
			var index=1;
			result = result.result.spark.nodes;
			for(var i =0;i<result.length;i++){
				for(var item in result[i]){
					if(result[i][item].state==1){
						var appendStr="<tr><td><input id='"+index+"' type='checkbox' class='minimal' name='chk_list'>"+item+"</td></tr>";
						keyArray[index]=item;
						$("#example1").append(appendStr);
						index = parseInt(index)+1;
					}
				}
			}
		}
	}); 
}

function addUser(){
	if($("#sysRole").val()==""){
		alert("请选择角色");
		return;
	}
	if($("#user").val()==""){
		alert("用户名不能为空");
		return;
	}
	if($("#password").val()==""){
		alert("密码不能为空");
		return;
	}
	if($("#password").val()!=$("#rePassword").val()){
		alert("两次密码输入要相同");
		return;
	}
	if($("#limitFlow").val()==""){
		alert("流量限制不能为空");
		return;
	}
	if($("#limitNum").val()==""){
		alert("条数限制不能为空");
		return;
	}
	if($("#limitSpace").val()==""){
		alert("总量限制不能为空");
		return;
	}
	
	var limitNode =new Array();
	var i = 0;
	$("input[name='chk_list']").each(
			function(){
				if($(this).prop("checked")){
					limitNode[i] = keyArray[$(this).attr("id")];
					i  = parseInt(i)+1;
				}
			}		
		);
	$("#limitNode").val(limitNode);
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		dataType: "json",
		traditional: true,  
		url: path+'manager/addUser',
		data:$('#userForm').serialize(),
		//data:{limitNode:limitNode,sysRole:sysRole,serviceName:serviceName,ip:ip},
		success: function(result2){
			if(result2){
				alert("添加成功");
				history.go(-1); 
				location.reload(); 
			}else{
				alert("添加失败");
				history.go(-1); 
				location.reload(); 
			}
		}
	});
	
}

</script>
<body class="hold-transition skin-blue sidebar-mini">

	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>用户管理<small>新增/编辑用户</small></h1>
				<ol class="breadcrumb">
					<li><a href="javascript:;"><i class="fa fa-dashboard"></i> 首页</a></li>
					<li class="active">新增/编辑用户</li>
				</ol>
			</section>
			<!-- Main content -->
			<section class="content">
	            <!-- form start -->
				<form id="userForm">
				<div class="row">
					<div class="col-md-12">
					<div class="box box-info">
			            <div class="box-header with-border">
			              <h3 class="box-title">用户信息 </h3>
			            </div>
			            <!-- /.box-header -->
			              <div class="box-body form-horizontal">
			              	<div class="form-group">
			                  <label for="inputEmail3" class="col-sm-2 control-label">角色</label>
			                  <div class="col-sm-3">
			                    <select name="type" class="form-control select2" id="sysRole">
										<option value="">==全部==</option>
										<option value="0">普通用户</option>
										<option value="1">配置管理员</option>
										<option value="2">审计管理员</option>
										<option value="3">系统管理员</option>
									</select>
			                  </div>
			                </div>
			                <div class="form-group">
			                  <label for="inputEmail3" class="col-sm-2 control-label">用户名</label>
			                  <div class="col-sm-3">
			                    <input name="user"  type="text" class="form-control" id="user" placeholder="用户名" >
			                  </div>
			                </div>
			                <div class="form-group">
			                  <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
			                  <div class="col-sm-3">
			                    <input name="password"  type="password" class="form-control" id="password" placeholder="密码">
			                  </div>
			                </div>
			                <div class="form-group">
			                  <label for="inputPassword4" class="col-sm-2 control-label">重复密码</label>
			                  <div class="col-sm-3">
			                    <input name="rePassword"  type="password" class="form-control" id="rePassword" placeholder="重复密码">
			                  </div>
			                </div>
			              <div class="form-group">
			                  <label for="inputEmail3" class="col-sm-2 control-label">流量限制</label>
			                  <div class="col-sm-3">
			                    <input name="limitFlow"  type="text" class="form-control" id="limitFlow" placeholder="流量">
			                  </div>MB/S
			                </div>
			                <div class="form-group">
			                  <label for="inputPassword3" class="col-sm-2 control-label">条数限制</label>
			                  <div class="col-sm-3">
			                    <input name="limitNum"  type="text" class="form-control" id="limitNum" placeholder="条数">
			                  	<input id="limitNode" name="limitNode" type="hidden" />
			                  </div>条/S
			                </div>
			                <div class="form-group">
			                  <label for="inputPassword4" class="col-sm-2 control-label">总量限制</label>
			                  <div class="col-sm-3">
			                    <input name="limitSpace"  type="password" class="form-control" id="limitSpace" placeholder="总量">
			                  </div>TB
			                </div>
			                <div class="form-group">
				                  <label for="inputPassword4" class="col-sm-2 control-label">分配节点</label>
				                  <div class="col-sm-3">
				                  	<div class="box-body table-bordered no-padding">
										<table id="example1" class="table table-hover">
											
										</table>
									</div>
				                  </div>
			                </div>
			                <div class="box-footer">
				                <a type="button" class="btn btn-info" onclick="addUser()">保存</a>
				                <a href="${url }manager/list" class="btn btn-default">取消</a>
				             </div>
				              <!-- /.box-body -->
			          	</div>
			          </div>
			        </div>
				</form>
			</section>
		</div>
	</div>
	<script type="text/javascript">
	$(function () {
		$(".select2").select2();
		//iCheck for checkbox and radio inputs
	    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
	      checkboxClass: 'icheckbox_minimal-blue',
	      radioClass: 'iradio_minimal-blue'
	    });
	});
	</script>
</body>